<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 面包屑导航 -->
      <div class="bread-container">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: `/category/${goods.categoryId}` }">{{ goods.categoryName }}</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: `/category/sub/${goods.subCategoryId}` }">{{ goods.subCategoryName }}</el-breadcrumb-item>
          <el-breadcrumb-item>{{ goods.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <!-- 商品信息 -->
      <div class="info-container">
        <div class="goods-info">
          <div class="media">
            <!-- 图片预览区 -->
            <XtxImageView v-if="goods.mainPictures" :image-list="goods.mainPictures" />
            <!-- 统计数量 -->
            <ul class="goods-sales">
              <li>
                <el-rate v-model="goods.rating" disabled text-color="#ff9900"/>
                <p><i class="iconfont icon-favorite-filling"></i>评分</p>
              </li>
              <li>
                <p>{{ goods.salesCount }}</p>
                <p><i class="iconfont icon-task-filling"></i>销量</p>
              </li>
              <li>
                <p>{{ goods.commentCount }}</p>
                <p><i class="iconfont icon-comment-filling"></i>评论数</p>
              </li>
              <li>
                <p>{{ goods.brand }}</p>
                <p><i class="iconfont icon-dynamic-filling"></i>品牌信息</p>
              </li>
            </ul>
          </div>
          <div class="spec">
            <!-- 商品信息区 -->
            <p class="g-name">{{ goods.name }}</p>
            <p class="g-desc">{{ goods.desc }}</p>
            <p class="g-price">
              <span>{{ goods.price }}</span>
              <span> {{ goods.oldPrice }}</span>
            </p>
            <div class="g-service">
              <dl>
                <dt>促销</dt>
                <dd>劲爆特惠！精选好物直降到底，限时抢购享不停</dd>
              </dl>
              <dl>
                <dt>服务</dt>
                <dd>
                  <span>无忧退货</span>
                  <span>快速退款</span>
                  <span>免费包邮</span>
                  <a href="javascript:;">了解详情</a>
                </dd>
              </dl>
            </div>
            <!-- sku组件 -->
            <XtxSku :goods="goods" @change="skuChange" />
            <!-- 数据组件 -->
            <el-input-number v-model="count" />
            <!-- 按钮组件 -->
            <div>
              <el-button size="large" class="btn" @click="addCart">加入购物车</el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 评论区 -->
      <div class="comment-container">
        <h3>用户评价</h3>
        <div v-if="goods.commentList && goods.commentList.length > 0">
          <div class="comment-item" v-for="(comment, index) in goods.commentList" :key="index">
            <div class="user-info">
              <span class="user-name">{{ comment.userName }}</span>
              <el-rate v-model="comment.rating" disabled show-score text-color="#ff9900" score-template="{value} 分" />
            </div>
            <div class="comment-content">
              {{ comment.content }}
            </div>
            <div class="comment-time">{{ comment.time }}</div>
          </div>
        </div>
        <div v-else class="no-comment">
          暂无评论
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getGoodsAPI } from "@/apis/goods";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { useCartStore } from "@/stores/cartStore";

const cartStore = useCartStore();
const goods = ref({});
const route = useRoute();

const getGoods = async () => {
  const res = await getGoodsAPI(route.params.id);
  goods.value = res.data;
};

onMounted(() => getGoods());

// sku规格被操作时
let skuObj = {};
const skuChange = (sku) => {
  skuObj = sku;
};

// count
const count = ref(1);

// 添加购物车
const addCart = () => {
  if (skuObj.skuId) {
    cartStore.addCart({
      id: goods.value.id,
      name: goods.value.name,
      picture: skuObj.picture,
      price: skuObj.price,
      count: count.value,
      skuId: skuObj.skuId,
      attrsText: skuObj.specsText,
      selected: true,
    });
  } else {
    ElMessage.warning("请选择规格");
  }
};
</script>

<style scoped lang="scss">
.xtx-goods-page {
  .goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;

    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
    }

    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
  }

  .g-name {
    font-size: 22px;
  }

  .g-desc {
    color: #999;
    margin-top: 10px;
  }

  .g-price {
    margin-top: 10px;

    span {
      &::before {
        content: "¥";
        font-size: 14px;
      }

      &:first-child {
        color: $priceColor;
        margin-right: 10px;
        font-size: 22px;
      }

      &:last-child {
        color: #999;
        text-decoration: line-through;
        font-size: 16px;
      }
    }
  }

  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;

    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;

      dt {
        width: 50px;
        color: #999;
      }

      dd {
        color: #666;

        &:last-child {
          span {
            margin-right: 10px;

            &::before {
              content: "•";
              color: $xtxColor;
              margin-right: 2px;
            }
          }

          a {
            color: $xtxColor;
          }
        }
      }
    }
  }

  .goods-sales {
    display: flex;
    width: 400px;
    align-items: center;
    text-align: center;
    height: 140px;

    li {
      flex: 1;
      position: relative;

      ~li::after {
        position: absolute;
        top: 10px;
        left: 0;
        height: 60px;
        border-left: 1px solid #e4e4e4;
        content: "";
      }

      p {
        &:first-child {
          color: #999;
        }

        &:nth-child(2) {
          color: $priceColor;
          margin-top: 10px;
        }

        &:last-child {
          color: #666;
          margin-top: 10px;

          i {
            color: $xtxColor;
            font-size: 14px;
            margin-right: 2px;
          }

          &:hover {
            color: $xtxColor;
            cursor: pointer;
          }
        }
      }
    }
  }

  .comment-container {
    margin-top: 40px;
    padding: 30px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);

    h3 {
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 20px;
      color: #333;
    }

    .comment-item {
      margin-bottom: 20px;
      padding-bottom: 20px;
      border-bottom: 1px solid #eee;

      .user-info {
        display: flex;
        align-items: center;
        margin-bottom: 10px;

        .user-name {
          font-weight: bold;
          margin-right: 10px;
          color: #333;
        }
      }

      .comment-content {
        color: #555;
        line-height: 1.6;
        font-size: 14px;
      }

      .comment-time {
        margin-top: 10px;
        font-size: 12px;
        color: #999;
      }
    }

    .no-comment {
      text-align: center;
      color: #999;
      font-size: 14px;
    }
  }

  .btn {
    margin-top: 20px;
  }

  .bread-container {
    padding: 25px 0;
  }
}
</style>